﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.SYST.Utility;
using Sector7.Restaurant.Modules.SYST.LocalService;
using Sector7.Restaurant.Biz;

namespace Sector7.Restaurant.Modules.SYST.Windows
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : UserControl
    {
        /// <summary>
        /// 登录成功事件
        /// </summary>
        public event EventHandler LoginSuccess = null;

        public LoginWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 触发登录成功事件
        /// </summary>
        /// <param name="e"></param>
        protected void OnLoginSuccess(EventArgs e)
        {
            if (LoginSuccess != null)
                LoginSuccess(this, e);
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string message = string.Empty;

            string userName = txbUserName.Text;
            string password = txbPassword.Password;

            if (userName.Length == 0 || password.Length == 0)
            {
                message = "用户名或密码不能为空。";
            }
            else
            {
                var loginAccount = Aide.LocalClient.Invoke<RS_Employee>(
                    service => service.LoginSystem(userName, password));

                if (null != loginAccount)
                {
                    Environments.CurrentAccount = loginAccount;

                    OnLoginSuccess(new EventArgs());
                }
                else
                {
                    message = "登录失败，未找到指定用户。";
                }
            }
            
            lblError.Content = message;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Environments.CurrentCompany = Aide.LocalClient.Invoke<S7_Company>(
                service => service.GetCompany("00000000-0000-0000-0000-000000000000"));

            this.DataContext = Environments.CurrentCompany;
        }
    }
}
